package com.cmii.sjw.department.control.jsc.service.impl;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.hutool.http.Header;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cmii.sjw.department.control.jsc.config.OneFormConfig;
import com.cmii.sjw.department.control.jsc.mapper.DistrictKpiMapper;
import com.cmii.sjw.department.control.jsc.mapper.PerformanceKpiMapper;
import com.cmii.sjw.department.control.jsc.model.DistrictKpi;
import com.cmii.sjw.department.control.jsc.service.OneFormService;
import com.cmii.sjw.department.control.jsc.service.StudentsService;
import com.cmii.sjw.department.control.jsc.util.Result;
import com.cmii.sjw.department.control.jsc.vo.DistrictKpiDTO;
import com.cmii.sjw.department.control.jsc.vo.DistrictKpiVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author lz
 * @Date 2025-03-20 周四 10:00
 * @Version 1.0
 */
@Slf4j
@Service
public class StudentsServiceImpl implements StudentsService {

    @Autowired
    private OneFormConfig ybtConfig;

    @Autowired
    private PerformanceKpiMapper performanceKpiMapper;

    @Autowired
    private DistrictKpiMapper districtKpiMapper;

    @Autowired
    private SaTokenDao dao;

    private static final int TOKEN_EXPIRE_SECONDS = 7140;


    public String getToken() {
        String token = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/auth/getToken");
            uriBuilder.addParameter("appId", ybtConfig.getAppId());
            uriBuilder.addParameter("secretKey", ybtConfig.getSecretKey());
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();
            log.info("一表通Token ,url:{},apiResult:{}", url, apiResult);
            com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

            if ("0".equals(content.getObject("code", String.class))) {
                log.info("一表通Token结果:{}", content.getString("data"));
                token = content.getJSONObject("data").get("access_token").toString();
            } else {
                log.error("一表通Token结果异常 responseCode:{}", content.getString("code"));
            }
        } catch (Exception e) {
            log.error("一表通Token请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
        }
        return token;
    }

    @Override
    public String TokenManager() {
        String token = dao.get("one-form:token");
        if (StringUtils.isEmpty(token)) {
            synchronized (this) {
                token = dao.get("one-form:token"); // 双重检查
                if (StringUtils.isEmpty(token)) {
                    try {
                        token = getToken();
                        log.info("获取新的一表通Token:{}", token);
                        dao.set("one-form:token", token, TOKEN_EXPIRE_SECONDS);
                    } catch (Exception e) {
                        log.error("生成Token失败", e);
                        throw new RuntimeException("Token生成异常"); // 根据业务需求处理异常
                    }
                }
            }
        }
        return token;
    }

    public Result mentaHeath(String year,String type, String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/mentaHeath");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("type", type);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result mentaHeathDetail(String year, String type, String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/mentaHeathDetail");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("type", type);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result visionHeath(String year) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/visionHeath");
            uriBuilder.addParameter("year", year);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result visionHeathDetail(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/visionHeathDetail");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result physicalHealthDetail(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/physicalHealthDetail");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result physicalHealth(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/physicalHealth");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result periodStudent(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/periodStudent");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result studentDetail(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/studentDetail");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result town(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/town");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result periodGender(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/periodGender");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result enrolled(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/student/enrolled");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result physical(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/student/physical");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result degree(String year) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/student/degree");
            uriBuilder.addParameter("year", year);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result schoolDetail(String year,String orgnazitionNo) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/student/detail");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("orgnazitionNo", orgnazitionNo);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result variousTypes(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/student/variousTypes");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result alteration(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/student/alteration");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result countyStudentNum(String year) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/student/countyStudentNum");
            uriBuilder.addParameter("year", year);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result layout(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/layout");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result averageArea(String year,String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/averageArea");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result summary(String year,String name,String type) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/summary");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("name", name);
            uriBuilder.addParameter("type", type);
            String url = uriBuilder.build().toString();
            log.info("一表通-指标汇总表请求:{}", url);

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();
            log.info("一表通-指标汇总表请求结果:{}", apiResult);
            JSONObject content = JSON.parseObject(apiResult);

            log.info("一表通-指标汇总表请求解析结果:{}", content);
            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
    public Result studentList(String year, String orgnazitionNo, int pageNum, int pageSize) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/student/studentList");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("orgnazitionNo", orgnazitionNo);
            uriBuilder.addParameter("pageNum", String.valueOf(pageNum));
            uriBuilder.addParameter("pageSize", String.valueOf(pageSize));
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            JSONObject content = JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }
}

